import React from "react";
import { asyncComponent } from "share/utils";
import debounce from "lodash.debounce";
import SlideFrame from "components/slide-frame";
import configureStore from "stores";

const ExpenseReportDetil = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(
      require("containers/expense-report/base-expense-report-detail").default
    );
  });
});
const BaseRequestDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/request/entrance/request-detail").default);
  });
});
const BaseRequestEdit = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/request/new-edit-request").default);
  });
});
const JDRequestDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/request/jd-request-edit").default);
  });
});
const LoanDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/loan-billing/loan-detail").default);
  });
});
const OtherDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/approve/other/other-detail").default);
  });
});
const Advertisement = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(
      require("containers/request-and-expense-settings/expense-setting/advertisement")
        .default
    );
  });
});
const FormSetting = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/setting/form/form-detail/form-detail").default);
  });
});
const ContractDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/contract/my-contract/contract-detail").default);
  });
});
const PaymentDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/payment-request/payment-details").default);
  });
});
const payCashDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/pay/pay-workbench/payment-detail").default);
  });
});
const ExpenseDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(require("containers/expense").default);
  });
});
const CorporateReportDetail = asyncComponent((resolve) => {
  require.ensure([], (require) => {
    resolve(
      require("containers/corporate-payment-report/detail/index").default
    );
  });
});
/**
 * 这个工具函数希望可以做到：
 * 1. 暴露四个单据详情侧拉框方法 openExpenseReport、openRequestReport、openLoanReport ，用{pageFrom, status, ...}等参数判断当前
 * 业务场景使得在不同业务场景下可以直接调用打开单据详情页而不用引入详情页组件和侧拉框组件，不用频繁的操作处理url，可以更关注业务逻辑而不
 * 用过多考虑怎么分辨场景；
 * 2. 暴露一个openSlideFrame方法，封装SlideFrame的open方法；
 * 3. 集中管控pageFrom参数，不要再随便乱加类似的参数，甚至有重复意义的参数
 */

const reportType = {
  expenseReport: ExpenseReportDetil, //报销单详情
  requestReport: BaseRequestDetail, //申请单非编辑状态详情
  requestEdit: BaseRequestEdit, //申请单编辑状态详情
  requestJD: JDRequestDetail, //京东申请单详情详情
  loanReport: LoanDetail, //借款单详情
  otherReport: OtherDetail, //通用单据详情
  advertisement: Advertisement, // 免打印广告页
  formSetting: FormSetting, //表单设置详情页
  contractDetail: ContractDetail,
  paymentDetail: PaymentDetail,
  payCashDetail: payCashDetail,
  expenseDetail: ExpenseDetail, // 费用详情
  corporateReport: CorporateReportDetail, //对公支付单详情
};

const openSlideFrame = debounce(
  (config, context) => SlideFrame.open(config, context),
  501,
  { leading: true, trailing: false }
);

const closeSlideFrame = () => {
  const openDeep = SlideFrame.destroyFns.length;
  for (let i = 0; i < openDeep; i++) {
    SlideFrame.destroyFns.pop()();
  }
};
const defaultParams = (entityType, params) => ({
  content: reportType[entityType],
  width: `calc(100vw - ${configureStore.store.getState().main.menuWidth}px)`,
  height: "calc(100vh - 50px)",
  className: `full-width-${configureStore.store.getState().main.menuWidth}`,
  hasMask: false,
  hasFooter: false,
  onClose: closeSlideFrame,
  params,
});

/**
 * 希望调用的时候只用考虑重要的业务参数expenseReportOID、formOID、approverOID等
 * @param businessParams 业务参数
 * @param slideFrameParams 侧拉框参数
 * @param context
 */
const openExpenseReport = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("expenseReport", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openRequestReport = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("requestReport", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openRequestReportEdit = function (
  businessParams,
  slideFrameParams,
  context
) {
  openSlideFrame(
    {
      ...defaultParams("requestEdit", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openRequestJDDetail = function (
  businessParams,
  slideFrameParams,
  context
) {
  openSlideFrame(
    {
      ...defaultParams("JDRequestDetail", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openLoanReport = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("loanReport", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openOtherReport = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("otherReport", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openAdvertisement = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("advertisement", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openFormSetting = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("formSetting", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openPaymentDetail = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("paymentDetail", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const contractDetail = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("contractDetail", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openPayCashDetail = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("payCashDetail", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openExpenseDetail = function (businessParams, slideFrameParams, context) {
  openSlideFrame(
    {
      ...defaultParams("expenseDetail", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

const openCorporateReportDetail = function (
  businessParams,
  slideFrameParams,
  context
) {
  openSlideFrame(
    {
      ...defaultParams("corporateReport", businessParams),
      ...slideFrameParams,
    },
    context
  );
};

export default {
  openSlideFrame,
  closeSlideFrame,
  openExpenseReport,
  openLoanReport,
  openRequestReport,
  openRequestReportEdit,
  openRequestJDDetail,
  openOtherReport,
  openAdvertisement,
  openFormSetting,
  contractDetail,
  openPaymentDetail,
  openPayCashDetail,
  openExpenseDetail,
  openCorporateReportDetail,
};
